package com.example.zoombar;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ZoomControls;

public class MainActivity extends Activity {

	private ZoomControls zoomControls;
	static float size = 12;
	private MyFirstTextView text;
	
	
 	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		 
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		setTitle("ZoomControls");
		zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
		// Log.i("zoom", ""+this.ge);
		text = (MyFirstTextView) findViewById(R.id.text);
		size= text.getTextSize();
		 
		

		showZoomControlsView(true); 
		initZoomControlsTimer();
		text.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				showZoomControlsView(true);
				
				return false;
			}
		});
		//TODO 提供3个模式,1.浏览模式(可以缩放字体,向右滑可以跳到下一条)
		//2.查词模式(点击一次,可以选中一个单词,点击并滑动可以选中多个连续的单词);
		//3.自由复制(像其他阅读器那样);
		//争取合并功能,3个模式有点太多了
//		text.setOnTouchListener(new ZoomListenter());
		zoomControls.setOnZoomInClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				size = size + 2;
				text.setTextSize(size);
			}
		});

		zoomControls.setOnZoomOutClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				size = size - 2;
				text.setTextSize(size);
			}

		});

	}

	int timerseconds = 1;
	Timer timer;
	Handler handler;
	/**
	 * init
	 */
	private void initZoomControlsTimer() {
		timer = new Timer();
		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:
 
					if (zoomControls.isShown() && timerseconds >= 3) {
						showZoomControlsView(false);

					}
					break;
				}
				super.handleMessage(msg);
			}
		};

		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				timerseconds++;
				Message message = new Message();
				message.what = 1;
				handler.sendMessage(message);
 
			}

		}, 1000, 1000);

	}
    
	
	int mode = 0;
	float oldDist=0;
	float textSize = 0;

	EditText textView = null;
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		timerseconds = 1;

		showZoomControlsView(true);
		
//		{
//
//			textView = text;
//			if (textSize == 0) {
//				textSize = textView.getTextSize();
//			}
//			switch (event.getAction() & MotionEvent.ACTION_MASK) {
//			case MotionEvent.ACTION_DOWN:
//				mode = 1;
//				break;
//			case MotionEvent.ACTION_UP:
//				mode = 0;
//				break;
//			case MotionEvent.ACTION_POINTER_UP:
//				mode -= 1;
//				break;
//			case MotionEvent.ACTION_POINTER_DOWN:
//				oldDist = spacing(event);
//				mode += 1;
//				break;
//
//			case MotionEvent.ACTION_MOVE:
//				Log.i("finger", "ACTION_MOVE:mode="+mode);
//				if (mode >= 2) {
//					float newDist = spacing(event);
//					if (newDist > oldDist + 1) {
//						zoom(textView,textSize,newDist / oldDist);
//						oldDist = newDist;
//					}
//					if (newDist < oldDist - 1) {
//						zoom(textView,textSize,newDist / oldDist);
//						oldDist = newDist;
//					}
//				}
//				break;
//			}
//			
//		}

		return false;
	}
	
	private void zoom(TextView textView,float textSize,float f) {
		textView.setTextSize(textSize *= f);
	}

	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}
	

	public void showZoomControlsView(Boolean boo) {
		if (boo) {
			if (!zoomControls.isShown()) {
				zoomControls.show();
				timerseconds=1;
			}
		} else {
			if (zoomControls.isShown()) {
				zoomControls.hide();
			}
		}
	}
	
	
//	class ZoomListenter implements OnTouchListener {
//
//		private int mode = 0;
//		float oldDist;
//		float textSize = 0;
//
//		TextView textView = null;
//
//		@Override
//		public boolean onTouch(View v, MotionEvent event) {
//			showZoomControlsView(true);
//			
//			Log.i("finger", "onTouch invoked="+(event.getAction() & MotionEvent.ACTION_MASK));
//			
//			textView = (TextView) v;
//			if (textSize == 0) {
//				textSize = textView.getTextSize();
//			}
//			switch (event.getAction() & MotionEvent.ACTION_MASK) {
//			case MotionEvent.ACTION_DOWN:
//				mode = 1;
//				break;
//			case MotionEvent.ACTION_UP:
//				mode = 0;
//				break;
//			case MotionEvent.ACTION_POINTER_UP:
//				mode -= 1;
//				break;
//			case MotionEvent.ACTION_POINTER_DOWN:
//				oldDist = spacing(event);
//				mode += 1;
//				break;
//
//			case MotionEvent.ACTION_MOVE:
//				Log.i("finger", "ACTION_MOVE:mode="+mode);
//				if (mode >= 2) {
//					float newDist = spacing(event);
//					if (newDist > oldDist + 1) {
//						zoom(newDist / oldDist);
//						oldDist = newDist;
//					}
//					if (newDist < oldDist - 1) {
//						zoom(newDist / oldDist);
//						oldDist = newDist;
//					}
//				}
//				break;
//			}
//			return false;
//		}
//
//		private void zoom(float f) {
//			textView.setTextSize(textSize *= f);
//		}
//
//		private float spacing(MotionEvent event) {
//			float x = event.getX(0) - event.getX(1);
//			float y = event.getY(0) - event.getY(1);
//			return FloatMath.sqrt(x * x + y * y);
//		}
//
//	}

}